import {getDictInfo} from '@/utils/LocalDataUtil'

export const getDictDataOption = keyName => {
  let dictData = getDictInfo()
  let option = []
  if (dictData) {
    for (let i = 0; i < dictData.length; i++) {
      if (dictData[i].keyName === null) continue
      if (dictData[i].keyName === keyName) {
        let item = {}
        item.name = dictData[i].name
        item.code = dictData[i].code
        if (dictData[i].children && dictData[i].children !== null) {
          item.children = []
          for (let k = 0; k < dictData[i].children.length; k++) {
            let childItem = {}
            childItem.name = dictData[i].children[k].name
            childItem.code = dictData[i].children[k].code
            item.children.push(childItem)
          }
        }
        option.push(item)
      }
    }
  }
  return option
}

export const getDictDataTree = _ => {
  let dictData = getDictInfo()
  let treeData = []
  for (let i = 0; i < dictData.length; i++) {
    if (treeData.length !== 0) {
      for (let j = 0; j < treeData.length; j++) {
        if (treeData[j].name === dictData[i].keyName) {
          let childrenObj = {}
          childrenObj.name = dictData[i].name
          childrenObj.id = dictData[i].code
          treeData[j].children.push(childrenObj)
        } else if (j === (treeData.length - 1)) {
          let keyObj = {}
          keyObj.name = dictData[i].keyName
          keyObj.id = dictData[i].keyCode
          keyObj.children = []
          let childrenObj = {}
          childrenObj.name = dictData[i].name
          childrenObj.id = dictData[i].code
          keyObj.children.push(childrenObj)
          treeData.push(keyObj)
        }
      }
    } else {
      let keyObj = {}
      keyObj.name = dictData[i].keyName
      keyObj.id = dictData[i].keyCode
      keyObj.children = []
      let childrenObj = {}
      childrenObj.name = dictData[i].name
      childrenObj.id = dictData[i].code
      keyObj.children.push(childrenObj)
      treeData.push(keyObj)
    }
  }
  let data = []
  data.push({name: '国标字典', id: '000', children: treeData})
  return data
}
